'''
Created on Nov 27, 2011

@author: Nich
'''
import uuid

from PyMud.model.base import Base
from sqlalchemy import Column, String, ForeignKey
from sqlalchemy.orm import relationship



class Account(Base):
    __tablename__ = "account"
    
    id = Column(String, primary_key=True)
    first_name = Column(String)
    last_name = Column(String)
    email = Column(String, unique = True)
    avatars = relationship("AccountAvatar")
    
    
    def __init__(self, first_name, last_name, email):
        self.id = str(uuid.uuid4())
        self.first_name = first_name
        self.last_name = last_name
        self.email = email

class AccountAvatar(Base):
    __tablename__ = "account_avatar"
    
    account_id = Column(String, ForeignKey("account.id"), primary_key = True)
    avatar_id = Column(String, ForeignKey("entity.id"), primary_key = True)
    avatar = relationship("Avatar")
    
class AccountUtils():
    def __init__(self, Session, avatar_factory):
        self.Session = Session
        self.avatar_factory = avatar_factory
    
    def print_all(self):
        session = self.Session()
        for instance in session.query(Account).order_by(Account.account_id): 
            print(instance.name)
            
    def get(self, email):
        session = self.Session()
        try:
            return session.query(Account).filter(Account.email==email).one()
        except:
            return None
        
    def get_by_id(self, id):
        session = self.Session()
        return session.query(Account).filter(Account.id==id).one()
        
    
    def make_account(self, first_name, last_name, email):
        session = self.Session()
        a = Account(first_name, last_name, email)
        session.add(a)
        session.commit()
        return a
    
    def create_new_avatar_for_account(self, account_id):
        session = self.Session()
        
        a = self.avatar_factory.create_default_avatar(session = session)
        account = session.query(Account).filter(Account.id==account_id).one()
        
        ac_av = AccountAvatar()
        ac_av.account_id = account.id
        ac_av.avatar = a
        account.avatars.append(ac_av)
        
        session.merge(ac_av)
        
        session.commit()
        
    def get_avatars_for_account(self, account):
        session = self.Session()
        session.add(account)
        
        return account.avatars
        
    def set_avatars_pid(self, avatar_id, player_id):
        from PyMud.objects.components import PlayerControlled
        from sqlalchemy import update
        session = self.Session()
        ex = update(PlayerControlled.__table__).where(PlayerControlled.entity_id==avatar_id).values(pid=player_id)
        session.execute(ex)
        session.commit()
    
    
    def handle_login(self, user, player_factory):
        db_user = self.get(user["email"])
        if db_user is None:
            db_user = self.make_account(user["first_name"], user["last_name"], user["email"])
        return db_user
    
#Base.metadata.create_all(engine)